#ifndef SERVICELOCATOR_H_
#define SERVICELOCATOR_H_

#include <cstring>
#include <map>
#include <typeinfo>
#include <utility>

#include "IBaseService.h"

class ServiceLocator
{
public:
	// Constructor and destructor.

	ServiceLocator() {};

	virtual ~ServiceLocator()
	{
		for (ServicesMap::iterator iterator = m_services.begin();
				iterator != m_services.end(); iterator++)
		{
			delete iterator->second;
		}
	};

	// Methods.

	template<class TInterface, class TService>
	void registerService(TService *service)
	{
		std::string key = typeid(TInterface).name();
		m_services.insert(make_pair(key, service));
	}

	template<class TInterface>
	TInterface *getService()
	{
		std::string key = typeid(TInterface).name();
		ServicesMap::iterator foundIterator = m_services.find(key);
		if (foundIterator == m_services.end())
		{
			return NULL;
		}

		IBaseService *baseService = foundIterator->second;
		TInterface *service = dynamic_cast<TInterface*>(baseService);

		return service;
	}

private:
	typedef std::map<std::string, IBaseService*> ServicesMap;
	ServicesMap m_services;
};

#endif /* SERVICELOCATOR_H_ */
